package servlet;

import domain.Student;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.WebContext;
import util.TemplateEngineUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        TemplateEngine engine = TemplateEngineUtil.getTemplateEngine(req.getServletContext());
        WebContext context = new WebContext(req, resp, req.getServletContext());
        resp.setCharacterEncoding("utf-8");
//        String username=req.getParameter("username");  //1.用于输入后显示
//        req.setAttribute("username",username);  //1.

//        String username=req.getParameter("username");  //2. 3.3WORD
//        String password=req.getParameter("password");  //2.
//        Student student=new Student();  //2.
//        student.setName(username);  //2.
//        student.setPassword(password);  //2.
//        student.setAge(Integer.parseInt(req.getParameter("age")));  //2.
//        //deliver student object to showUsername.html  //2.
//        req.setAttribute("student",student);  //2.

        String username=req.getParameter("username");
        String password=req.getParameter("password");
        String age1=req.getParameter("age");
        Student student1=new Student();
        student1.setName(username);
        student1.setPassword(password);
        student1.setAge(Integer.parseInt(req.getParameter("age")));
        List students=new ArrayList();  //创建数组
        students.add(student1);

        Student student2=new Student();
        student2.setName("athena");
        student2.setPassword("123");
        student2.setAge(20);
        students.add(student2);

        Student student3=new Student();
        student3.setName("hela");
        student3.setPassword("123");
        student3.setAge(50);
        students.add(student3);
        //deliver students to showUsername.html
        req.setAttribute("students",students);

        engine.process("showUsername.html",context,resp.getWriter());
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
